package com.guang.springbootthread.test2;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Description 测试如何来运行Callable代码
 * @Author liguang
 * @Date 2023/06/19/12:14
 */
public class NewCallable {

    private static class MyCallable implements Callable<Integer>{
        private int sum = 0;
        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程是："+Thread.currentThread().getName());
            for (int i = 0; i < 10000; i++) {
                sum=sum+1;
            }
            return sum;
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable myCallable = new MyCallable();
        // 包装成Runnable
        FutureTask<Integer> integerFutureTask = new FutureTask<>(myCallable);
        new Thread(integerFutureTask,"callableThread").start();
        System.out.println("当前线程是："+Thread.currentThread().getName());
        Integer sum = integerFutureTask.get();
        System.out.println("获取得到callable中的执行结果"+sum);
    }
}
